package com.wfs.myapp.controller.bk;

import com.wfs.myapp.common.ApiResult;
import com.wfs.myapp.common.Constant;
import com.wfs.myapp.entity.dto.req.bk.AddBookReqDto;
import com.wfs.myapp.service.BkBookService;
import com.wfs.myapp.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/bk/book")
@Api(value = "书籍", tags = "书籍")
@Slf4j
public class BookController {

    @Autowired
    private BkBookService bkBookService;

    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "新建/修改")
    public ApiResult<?> addOrUpdate(@Valid @RequestBody AddBookReqDto addBookReqDto, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkBookService.addOrUpdate(userId, addBookReqDto);
    }

    @GetMapping("/myBookList")
    @ApiOperation(value = "我的书列表")
    public ApiResult<?> myBookList(@RequestParam("page") Integer page,
                                    @RequestParam("size") Integer size,
                                    HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkBookService.myBookList(userId, page, size);
    }

    @GetMapping("/myPendingBookList")
    @ApiOperation(value = "我的书列表")
    public ApiResult<?> myPendingBookList(@RequestParam("page") Integer page,
                                   @RequestParam("size") Integer size,
                                   HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkBookService.myPendingBookList(userId, page, size);
    }

    @GetMapping("/bookListByUserId")
    @ApiOperation(value = "好友的书列表")
    public ApiResult<?> bookListByUserId(@RequestParam("userId") Long userId) {
        return bkBookService.bookListByUserId(userId);
    }

    @GetMapping("/bookInfo")
    @ApiOperation(value = "书籍详情")
    public ApiResult<?> bookInfo(@RequestParam("id") Long id, HttpServletRequest request) {
        return bkBookService.bookInfo(id);
    }

}
